#include "Images.h"
#include <QJsonObject>
#include <QImage>
#include <QDir>
#include <QFileInfo>


void Images::handleSession()
{
//    [4452] url:  "/images/B827EB586D40-0.jpg:1"
//    [300] strUrl:   "/images/B827EB586D40-2.jpg:1"
//    [2216] strUrl:   "/images/-0.jpg:1"


    QString strUrl = m_session->requestUrl();

    qDebug() << "strUrl:  " << strUrl;

    QString strImageName = strUrl.split("/").last();
    strImageName = strImageName.split(":").first();


    QString strMainMac = strImageName.split(".").first();  // B827EB586D40-2
    QStringList listMacIndex = strMainMac.split("-");
    strMainMac = listMacIndex.first();
    int index = listMacIndex.last().toInt();
    qDebug() << "strMainMac: " << strMainMac;
    qDebug() << "index: " << index;

    if (strMainMac.simplified().isEmpty())
    {
        return;
    }

    QString strImgDir = "pics/"+strMainMac;
    QDir dir(strImgDir);
    QFileInfoList listFileInfo = dir.entryInfoList(QStringList()<<"*.jpg", QDir::Files, QDir::Name);


//    QStringList listxx;
//    for (int i=0; i<listFileInfo.size(); ++i)
//    {
//        listxx << listFileInfo.at(i).filePath();
//    }

    if (index >= listFileInfo.size())
    {
        QString strTestImgPath = "pics/test/test.jpg";
        m_session->replyImage(strTestImgPath);
    }
    else
    {
        QString strImagePath = listFileInfo.at(index).absoluteFilePath();
        m_session->replyImage(strImagePath);
    }
}
